'''
Created on 2011-06-22

@author: Andrew Roth
'''
import pysam

from bam_counter.base_counter import BaseCounter
from bam_counter.multi_counter import MultiCounter
from python_multi_counter import MultiCounter as PythonMultiCounter

def cython_profile():
    normal_file_name = "/share/data/VBA0038/VBA0038.normal.bam"
    tumour_file_name = "/share/data/VBA0038/VBA0038.tumour.bam"

    normal_counter = BaseCounter(pysam.Samfile(normal_file_name, 'rb'))
    tumour_counter = BaseCounter(pysam.Samfile(tumour_file_name, 'rb'))
    
    multi_counter = MultiCounter([normal_counter, tumour_counter], 2)
    
    for i, row in enumerate(multi_counter.iter_ref('1')):
        if i >= int(2e4):
            break

def python_profile():
    normal_file_name = "/share/data/VBA0038/VBA0038.normal.bam"
    tumour_file_name = "/share/data/VBA0038/VBA0038.tumour.bam"

    normal_counter = BaseCounter(pysam.Samfile(normal_file_name, 'rb'))
    tumour_counter = BaseCounter(pysam.Samfile(tumour_file_name, 'rb'))
    
    multi_counter = PythonMultiCounter([normal_counter, tumour_counter], 2)
    
    for i, row in enumerate(multi_counter.iter_ref('1')):
        if i >= int(2e4):
            break

def profile():
    normal_file_name = "/share/data/VBA0038/VBA0038.normal.bam"
    tumour_file_name = "/share/data/VBA0038/VBA0038.tumour.bam"

    normal_counter = BaseCounter(pysam.Samfile(normal_file_name, 'rb'))
    tumour_counter = BaseCounter(pysam.Samfile(tumour_file_name, 'rb'))
    
    multi_counter = MultiCounter([normal_counter, tumour_counter], 2)
    
    for row in multi_counter.iter_ref('Y'):
        pass
            
if __name__ == "__main__":
#    cython_python()
#    visual_test()

#    import pstats, cProfile
#    
#    cProfile.runctx("cython_python()", globals(), locals(), "Profile.prof")
#    
#    s = pstats.Stats("Profile.prof")
#    s.strip_dirs().sort_stats("time").print_stats()
    
#    import line_profiler
#    
#    
#    from bam_counter.base_counter import BaseCounterIter
#    from bam_counter.multi_counter import MultiCounterIterator
#    
#    profiler = line_profiler.LineProfiler(MultiCounterIterator.next)
#    
#    run_str = "profile()"
#    
#    profiler.run( run_str )
#    profiler.print_stats()
#    
    import timeit
    
    python = "python_profile()"
    cython = "cython_profile()"
    
    t = timeit.Timer(python, "from __main__ import python_profile")
    print t.timeit(10)
    
    t = timeit.Timer(cython, "from __main__ import cython_profile")
    print t.timeit(10)
